﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VOL;
using BLL;

namespace Shopper
{
    public partial class frmNhaCungCapManager : DevExpress.XtraEditors.XtraForm
    {
        NhaCungCapBL nccBl = new NhaCungCapBL();
        public frmNhaCungCapManager()
        {
            InitializeComponent();
        }

        private void frmNhaCungCapManager_Load(object sender, EventArgs e)
        {
            FillData();
        }

        private void FillData()
        {
            gridControl1.DataSource = nccBl.GetData();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            using (frmNhaCungCapForm frm = new frmNhaCungCapForm())
            {
                if (frm.ShowDialog(this.FindForm()) == System.Windows.Forms.DialogResult.OK)
                {
                    NhaCungCap obj = new NhaCungCap();
                    obj.TenNhaCungCap = frm.TenNhaCungCap;
                    obj.DiaChi = frm.DiaChi;
                    obj.DienThoai = frm.DienThoai;
                    obj.Email = frm.Email;

                    nccBl.insertNhaCungCap(obj);

                    XtraMessageBox.Show("Thêm nhà cung cấp thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            using (frmNhaCungCapForm frm = new frmNhaCungCapForm())
            {
                int MaNhaCungCap = (int)gridView1.GetFocusedRowCellValue("MaNhaCungCap");
                DataTable dt = nccBl.searchIDNhaCungCap(MaNhaCungCap);
                frm.TenNhaCungCap = dt.Rows[0]["TenNhaCungCap"].ToString();
                frm.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                frm.DienThoai = dt.Rows[0]["DienThoai"].ToString();
                frm.Email = dt.Rows[0]["Email"].ToString();

                if (frm.ShowDialog(this.FindForm()) == System.Windows.Forms.DialogResult.OK)
                {
                    NhaCungCap obj = new NhaCungCap();
                    obj.TenNhaCungCap = frm.TenNhaCungCap;
                    obj.DiaChi = frm.DiaChi;
                    obj.DienThoai = frm.DienThoai;
                    obj.Email = frm.Email;
                    obj.MaNhaCungCap = MaNhaCungCap;

                    nccBl.updateNhaCungCap(obj);
                    XtraMessageBox.Show("Thay đổi nhà cung cấp thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    FillData();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult dr = XtraMessageBox.Show("Bạn có muốn xóa bản ghi này không ?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == System.Windows.Forms.DialogResult.Yes)
            {
                int MaNhaCungCap = (int)gridView1.GetFocusedRowCellValue("MaNhaCungCap");
                nccBl.deleteNhaCungCap(MaNhaCungCap);
                FillData();
            }
        }
    }
}